package VisitorPattern.example;

import VisitorPattern.example.framework.Visitor;

import java.util.Iterator;

public class ListVisitor extends Visitor {
    private String current_dir = "";

    @Override
    public void visit(File file) {
        System.out.println(current_dir + "/" + file.getName());
    }

    @Override
    public void visit(Directory directory) {
        System.out.println(current_dir + "/" + directory.getName());
        String save_dir = current_dir;
        current_dir = current_dir + "/" + directory.getName();
        Iterator<Entry> it = directory.iterator();
        while (it.hasNext()) {
            Entry entry = it.next();
            entry.accept(this);
            // 这里不能用this.visit(entry),因为visit不能判断传进来的参数到底是什么
        }
        current_dir = save_dir;
    }

    @Override
    public void visit(FileSystem fileSystem) {
        System.out.println(fileSystem.getName());
        visit(fileSystem.getRoot());
    }
}
